package com.wzdigit.wms.wms.controller.outbound;


import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.Queryer;
import com.wzdigit.wms.basic.client.request.picktask.PicktaskMangementRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.UserWhsZoneWorkDivisionResponse;
import com.wzdigit.wms.basic.client.response.picktask.PicktaskMangementResp;
import com.wzdigit.wms.basic.client.response.transfer.InvZoneDto;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.outbound.PicktaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 拣货任务管理_PC 前端控制器
 * </p>
 *
 * @author yangwenbiao
 * @since 2021-02-26
 */

@Slf4j
@RestController
@RequestMapping(value = WmsWareConstant.API_VERSION)
@Api(tags = "拣货任务管理_PC")
public class PicktaskManagementController {

    public static final String REQUEST_MODEL = "/picktask_management";

    @Autowired
    PicktaskService picktaskService;


    @ApiOperation(value = "查询")
    @GetMapping(value = REQUEST_MODEL)
    public PagerInfo<List<PicktaskMangementResp>> getTaskMmanagementInfo(Pager<?> page,
                                                                         PicktaskMangementRequest picktaskMangementRequest,
                                                                         HttpServletRequest servletRequest,
                                                                         HttpServletResponse servletResponse) throws Exception {
        try {
            Queryer<PicktaskMangementRequest> requestQueryer = new Queryer<>();
            requestQueryer.setPage(page);
            requestQueryer.setRequest(picktaskMangementRequest);

            PagerInfo<List<PicktaskMangementResp>> pagerInfo = picktaskService.getTaskMmanagementInfo(requestQueryer, servletRequest, servletResponse);
            return pagerInfo;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "查询改派任务的用户与仓库库区关联信息")
    @GetMapping(value = REQUEST_MODEL + "/getUserWhsZoneWorkDivision/{picktaskDtlId}/{filter}")
    public List<UserWhsZoneWorkDivisionResponse> getUserWhsZoneWorkDivision(
            @ApiParam("拣货任务明细ID") @PathVariable("picktaskDtlId") Integer picktaskDtlId,
            @ApiParam("用户名/账号模糊查询") @PathVariable(value = "filter", required = false) String filter,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception {
        try {
            return picktaskService.getUserWhsZoneWorkDivision(picktaskDtlId, filter);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @ApiOperation(value = "改派任务查询用户与仓库关联信息")
    @PutMapping(value = REQUEST_MODEL + "/updateTaskReceiver/{picktaskDtlId}/{username}")
    public Result<Integer> updateTaskReceiver(@ApiParam("拣货任务明细ID") @PathVariable("picktaskDtlId") Integer picktaskDtlId,
                                              @ApiParam("用户名") @PathVariable("username") String username,
                                              HttpServletRequest servletRequest,
                                              HttpServletResponse servletResponse) throws Exception {
        try {

            Result result = picktaskService.updateTaskReceiver(picktaskDtlId, username, servletRequest, servletResponse);

            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "取消拣货任务")
    @PostMapping(value = REQUEST_MODEL + "/cancel/picktask")
    public Result cancelPicktask(@ApiParam("拣货任务明细ID") @RequestBody List<Integer> picktaskDtlIdList,
                                                                 HttpServletRequest servletRequest,
                                                                 HttpServletResponse servletResponse) throws Exception {
        try {
            Result result = picktaskService.cancelPicktask(picktaskDtlIdList, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @ApiOperation(value = "关闭拣货任务")
    @PostMapping(value = REQUEST_MODEL + "/close/picktask")
    public Result closePicktask(@ApiParam("拣货任务明细ID") @RequestBody List<Integer> picktaskDtlIdList,
                                 HttpServletRequest servletRequest,
                                 HttpServletResponse servletResponse) throws Exception {
        try {
            Result result = picktaskService.closePicktask(picktaskDtlIdList, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }



    @ApiOperation(value = "拆分查询库区信息")
    @GetMapping(value = REQUEST_MODEL + "/split/zone/{picktaskNo}/{picktaskLineNo}/{gdnNo}/{whsId}")
    public Result<List<InvZoneDto>> splitGetZone(@ApiParam("拣货任务号") @PathVariable("picktaskNo") String picktaskNo,
                                                 @ApiParam("拣货任务行号") @PathVariable("picktaskLineNo") String picktaskLineNo,
                                                 @ApiParam("发货单号") @PathVariable("gdnNo") String gdnNo,
                                                 @ApiParam("仓库ID") @PathVariable("whsId") Integer whsId,
                                                 HttpServletRequest servletRequest,
                                                 HttpServletResponse servletResponse) throws Exception {
        try {
            List<InvZoneDto> invZoneDtos = picktaskService.splitGetZone(picktaskNo, picktaskLineNo, gdnNo, whsId, servletRequest, servletResponse);
            Result<List<InvZoneDto>> result = new Result<>();
            result.setData(invZoneDtos);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }



    @ApiOperation(value = "拆分提交")
    @PostMapping(value = REQUEST_MODEL + "/split_task/zone/{picktaskNo}/{picktaskLineNo}/{gdnNo}/{whsId}/{qty}/{invZoneId}/{zoneId}")
    public Result splitTask(@ApiParam("拣货任务号") @PathVariable("picktaskNo") String picktaskNo,
                                              @ApiParam("拣货任务行号") @PathVariable("picktaskLineNo") String picktaskLineNo,
                                              @ApiParam("发货单号") @PathVariable("gdnNo") String gdnNo,
                                              @ApiParam("仓库ID") @PathVariable("whsId") Integer whsId,
                                              @ApiParam("拆分数量") @PathVariable("qty") Integer qty,
                                              @ApiParam("库区库存ID") @PathVariable("invZoneId") Integer invZoneId,
                                              @ApiParam("库区ID") @PathVariable("zoneId") Integer zoneId,
                                              HttpServletRequest servletRequest,
                                              HttpServletResponse servletResponse) throws Exception {
        try {
            if (zoneId == -1) {
                zoneId = null;
            }
            Result result = picktaskService.splitTask(picktaskNo, picktaskLineNo, gdnNo, whsId, qty, invZoneId, zoneId, servletRequest, servletResponse);

            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

}